import {
  sidebar_flag,
  tag_list,
  setSession
} from '../../utils/config'

let app = {
  state: {
    sidebar: true,
    tagList: []
  },

  mutations: {
    SET_SIDEBAR: (state) => {
      state.sidebar = !state.sidebar
      setSession(sidebar_flag, state.sidebar)
    },
    SET_TAGLIST: (state, obj) => {
      let arr = state.tagList
      let flag = arr.some(item => item.url === obj.url)
      if (!flag) {
        arr.push(obj)
        state.tagList = arr
        setSession(tag_list, state.tagList)
      }
    },
    REMOVE_TAGLIST: (state, obj) => {
      let arr = state.tagList
      let index = arr.findIndex(item => item.url === obj.url)
      if (index > -1) {
        arr.splice(index, 1)
        state.tagList = arr
        setSession(tag_list, state.tagList)
      }
    },
    REMOVE_ALL: (state) => {
      state.tagList = []
      setSession(tag_list, state.tagList)
    }
  }
}

export default app